package czh.cocoa.foundation;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class NSDictionary extends NSObject {
	
	Map<NSObject, NSObject> mDictionary;
	
	public static NSDictionary dictionaryWithObjectsAndKeys(NSObject... objects) {
		NSDictionary dictionary = new NSDictionary();
		for (int i = 0; i < objects.length; i+=2) {
			NSObject value = objects[i];
			NSObject key = objects[i+1];
			dictionary.mDictionary.put(key, value);
		}
		return dictionary;
	}
	
	public NSDictionary() {
		this(3);
	}
	
	public NSDictionary(NSDictionary dictionary) {
		mDictionary = new HashMap<NSObject, NSObject>(dictionary.mDictionary);
	}
	
	public NSDictionary(NSArray objects, NSArray keys) {
		mDictionary = new HashMap<NSObject, NSObject>(); 
		for (int i = 0; i < objects.count(); i++) {
			NSObject value = objects.objectAtIndex(i);
			NSObject key = keys.objectAtIndex(i);
			mDictionary.put(key, value);
		}
	}
	
	protected NSDictionary(int capacity) {
		mDictionary = new HashMap<NSObject, NSObject>(capacity);
	}

	public NSObject objectForKey(NSObject key) {
		return mDictionary.get(key);
	}
	
	public NSArray allKeys() {
		Set<NSObject> keyset = mDictionary.keySet();
		NSArray keys = new NSArray();
		for (NSObject key : keyset) {
			keys.mArray.add(key);
		}
		return keys;
	}
	
	//additional
	public float floatForKey(NSObject key) {
		NSObject o = objectForKey(key);
		return o != null ? ((NSNumber)o).floatValue() : NSNotFound;
	}
	
	//additional
	public int intForKey(NSObject key) {
		NSObject o = objectForKey(key);
		return o != null ? ((NSNumber)o).intValue() : NSNotFound;
	}
	
	//additional
	public boolean boolForKey(NSObject key) {
		NSObject o = objectForKey(key);
		return o != null ? ((NSNumber)o).boolValue() : false;
	}

}
